<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\SmsChannel;
use app\common\model\SmsTemplate;
use app\common\model\TencentSms;
use app\common\model\TencentSmsLog;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function test()
    {
        $sms_config = (new SmsTemplate())->fetch(['key' => 'A19CE750-38E5-4409-AD57-0A9F07C65DD8 '])['data'];
        if (!$sms_config) {
            $this->error('sms_key不存在');
        }

        $sms_channel = (new SmsChannel())->fetch(['id' => $sms_config['sms_channel_id']])['data'];
        if (!$sms_channel) {
            $this->error('短信渠道配置错误');
        }
        $result = (new \app\common\model\TencentSms())->querySendStatus($sms_channel, '8615999996788');
        foreach ($result['PullSmsSendStatusSet'] as $record) {
            if ($record['ReportStatus'] == 'SUCCESS') {
                (new TencentSmsLog())->edit(['serial_no' => $record['SerialNo']], ['status' => 1]);
            } else {
                (new TencentSmsLog())->edit(['serial_no' => $record['SerialNo']], ['status' => 0, 'message' => $record['Description']]);
            }
        }
    }
}
